import os
from google.appengine.ext.webapp import template
from google.appengine.api import users

def isLoggedIn():
  return users.get_current_user()

def getLoginURLs(requestUri):
  result = {}
  if isLoggedIn():
    result['accountLoginUrl'] = users.create_logout_url(requestUri)
    result['accountLoginUrlLinktext'] = 'Logout'
    result['userIsLoggedIn'] = True
    result['accountNickname'] = users.get_current_user().nickname() 
  else:
    result['accountLoginUrl'] = users.create_login_url(requestUri)
    result['accountLoginUrlLinktext'] = 'Login'
    result['userIsLoggedIn'] = False
    result['accountNickname'] = 'Not logged in' 
  return result

def renderTemplate(requestUri, templateValues, templateFile):
  templateValuesToRender = templateValues
  loginURLs = getLoginURLs(requestUri)
  if templateValuesToRender is None:
    templateValuesToRender = loginURLs
  else:
    templateValuesToRender.update(loginURLs)

  path = os.path.join(os.path.join(os.path.dirname(__file__), '../../../'), templateFile)
  return template.render(path, templateValuesToRender)
